package com.bj58.ecdata.dashboard.databll;

import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.IncomeNetflowCateConst;
import com.bj58.ecdata.dashboard.constant.IncomeNetflowCityConst;
import com.bj58.ecdata.dashboard.constant.IncomePlatConst;
import com.bj58.ecdata.dashboard.dbentity.IncomeCateNetflow;
import com.bj58.ecdata.dashboard.dbentity.IncomeCityNetflow;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

public class IncomeNetflowBLL {

	private static AbstractBaseService<IncomeCityNetflow> icmCityService = ServiceBeanFactory.get(IncomeCityNetflow.class,null);
	private static AbstractBaseService<IncomeCateNetflow> icmCateService = ServiceBeanFactory.get(IncomeCateNetflow.class,null);
	
	/**
	 * 城市对应的产品(精准，置顶，智能)趋势图数据
	 */
	public static String getCityProJson(String dateGroup, String stat_type,String index, String businessName, String cityName){
		JSONObject ob = new JSONObject();
		String products[] ={"jingzhun","zhiding","zhineng"};
		String cols = IncomeNetflowCityConst.getCityProCol(dateGroup,index);
		String condition =  IncomeNetflowCityConst.getCityProCon(stat_type, businessName, cityName);
		List<IncomeCityNetflow> list = icmCityService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCityNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

		HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for(String pro : products){

				List<String> curList = dataMap.get(pro);
				if(curList == null)
					curList = Lists.newArrayList();
				IncomeCityNetflow vo = ReflectUtil.getByColVal(IncomeCityNetflow.class,stat_dateMap.get(date),"product_type",pro);
        		String val = (String) ReflectUtil.getVal(vo, index);
        		curList.add(val==null?"0":val);
				dataMap.put(pro, curList);
			}
		}
		ob.put("time", stat_dateMap.keySet());
		
//		List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//		for(String f : products){
//			List<String> dataList = new ArrayList<String>();
//			for(int i=0;i<dayList.size();i++){
//				dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
//			}
//			dataMap.put(f, dataList);
//		}
//		ob.put("time", dayList);
		ob.putAll(dataMap);

		return ob.toString();
	}
	
	/**
	 * echarts点击获取城市对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getCityProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
		String condition =  IncomeNetflowCityConst.getCityProTableCon(dateGroup, stat_type, businessName, dateVal);
		List<IncomeCityNetflow> list = icmCityService.getListByConditon( null, condition, "cost desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}
	

	/**
	 * 产品(精准|置顶|智能)的城市对比趋势图数据
	 */
	public static String getCityCmpJson(String dateGroup, String stat_type,String index, String businessName,String proType, String city1, String city2){
		JSONObject ob = new JSONObject();
		String[] citys={city1,city2};
		String cols = IncomeNetflowCityConst.getCityCmpCol(dateGroup, index);
		String condition =  IncomeNetflowCityConst.getCityCmpCon(stat_type, businessName,proType,city1,city2);
		List<IncomeCityNetflow> list = icmCityService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCityNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

		HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // key : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for (String ct : citys) {
				List<String> curList = dataMap.get(ct);
				if(curList == null)
					curList = Lists.newArrayList();
				IncomeCityNetflow cityVo = ReflectUtil.getByColVal(IncomeCityNetflow.class,stat_dateMap.get(date),"city_name",ct);
        		String val = (String) ReflectUtil.getVal(cityVo, index);
        		curList.add(val==null?"0":val);
				dataMap.put(ct, curList);
			}
		}
		ob.put("time", stat_dateMap.keySet());
		
//		List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//		for(String f : citys){
//			List<String> dataList = new ArrayList<String>();
//			for(int i=0;i<dayList.size();i++){
//				dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
//			}
//			dataMap.put(f, dataList);
//		}
//		ob.put("time", dayList);
		ob.putAll(dataMap);

		return ob.toString();
	}
	
	

    /**
     * 类别对应的产品(精准，置顶，智能)趋势图数据
     */
    public static String getCateProJson(String dateGroup, String stat_type,String index, String businessName, String cateName){
        JSONObject ob = new JSONObject();
        String products[] ={"jingzhun","zhiding","zhineng"};
        String cols = IncomeNetflowCateConst.getCateProCol(dateGroup,index);
        String condition =  IncomeNetflowCateConst.getCateProCon(stat_type, businessName, cateName);
        List<IncomeCateNetflow> list = icmCateService.getListByConditon( cols, condition, dateGroup);
        TreeMap<String, List<IncomeCateNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

        HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // stat_date : [dataList]
        for (String date : stat_dateMap.keySet()) {
        	for(String pro : products){
        		List<String> curList = dataMap.get(pro);
        		if(curList == null)
        			curList = Lists.newArrayList();
        		IncomeCateNetflow vo = ReflectUtil.getByColVal(IncomeCateNetflow.class,stat_dateMap.get(date),"product_type",pro);
        		String val = (String) ReflectUtil.getVal(vo, index);
        		curList.add(val==null?"0":val);
        		dataMap.put(pro, curList);
        	}
        }
      ob.put("time", stat_dateMap.keySet());
        
//        List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//        for(String f : products){
//            List<String> dataList = new ArrayList<String>();
//            for(int i=0;i<dayList.size();i++){
//                dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
//            }
//            dataMap.put(f, dataList);
//        }
//        ob.put("time", dayList);
        ob.putAll(dataMap);

        return ob.toString();
    }
    
    /**
     * echarts点击获取类别对应的产品(精准，置顶，智能) 表格展示数据
     */
    public static String getCateProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
      String condition =  IncomeNetflowCateConst.getCateProTableCon(dateGroup, stat_type, businessName, dateVal);
      List<IncomeCateNetflow> list = icmCateService.getListByConditon( null, condition, "cost desc");

      JSONObject ob = new JSONObject();
      ob.put("dataList", list);

        return ob.toString();
    }
    

    /**
     * 产品(精准|置顶|智能)的--类别---对比趋势图数据
     */
    public static String getCateCmpJson(String dateGroup, String stat_type,String index, String businessName,String proType, String cate1, String cate2){
        JSONObject ob = new JSONObject();
        String[] cates={cate1,cate2};
        String cols = IncomeNetflowCateConst.getCateCmpCol(dateGroup, index);
        String condition =  IncomeNetflowCateConst.getCateCmpCon(stat_type, businessName,proType,cate1,cate2);
        List<IncomeCateNetflow> list = icmCateService.getListByConditon( cols, condition, null);
        TreeMap<String, List<IncomeCateNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);
	
	    HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // key : [dataList]
	    for (String date : stat_dateMap.keySet()) {
	    	for (String ct : cates) {
	    		List<String> curList = dataMap.get(ct);
	    		if(curList == null)
	    			curList = Lists.newArrayList();
	    		IncomeCateNetflow cateVo = ReflectUtil.getByColVal(IncomeCateNetflow.class,stat_dateMap.get(date),"cate_name",ct);
        		String val = (String) ReflectUtil.getVal(cateVo, index);
        		curList.add(val==null?"0":val);
	    		dataMap.put(ct, curList);
	    	}
	    }
	    ob.put("time", stat_dateMap.keySet());
        
//        List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//        for(String f : cates){
//            List<String> dataList = new ArrayList<String>();
//            for(int i=0;i<dayList.size();i++){
//                dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
//            }
//            dataMap.put(f, dataList);
//        }
//        ob.put("time", dayList);
        ob.putAll(dataMap);

        return ob.toString();
    }
    
	/**
	 * 获取流量 0 级来源  “饼图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	public static String getSourcePieDataJson(String businessName,IncomePlatConst plat,String beginDate,String endDate){
		
		Class<?> beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSourcePieCon(businessName, beginDate, endDate);
		String columns = IncomePlatConst.getSourcePieCols();
		List<?> list = service.getListByPage(condition, columns, 1, 5, "cash desc");
		
		
		List<Object> source1List = Lists.newArrayList();
		List<Object> dataList = Lists.newArrayList();
		
		for (Object  sourceVo: list) {
			Object sourceName = ReflectUtil.getVal(sourceVo,"source");
			Object cost = ReflectUtil.getVal(sourceVo,"cash");
			JSONObject item = new JSONObject();
			item.put("value", cost);
			item.put("name", sourceName);
			dataList.add(item);
			source1List.add(sourceName);
		}
		
//		String[] sources = {"站内","站外免费","站外付费"};
//		Random random = new Random();
//		
//      for(String f : sources){
//			JSONObject item = new JSONObject();
//			item.put("value", random.nextInt(10000)+9000);
//			item.put("name", f);
//			dataList.add(item);
//			source1List.add(f);
//      }
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("sourceNameList", source1List);
		jsonObject.put("dataList", dataList);
		
		return jsonObject.toString();
	}	
	
	
	/**
	 * 获取流量1级来源  “饼图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	public static String getSource1PieDataJson(String businessName,String source,IncomePlatConst plat,String beginDate,String endDate){
		
		Class<?> beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSource1PieCon(businessName, source, beginDate, endDate);
		String columns = IncomePlatConst.getSource1PieCols();
		List<?> list = service.getListByPage(condition, columns, 1, 5, "cash desc");
		
		
		List<Object> source1List = Lists.newArrayList();
		List<Object> dataList = Lists.newArrayList();
		
		for (Object  sourceVo: list) {
			Object sourceName = ReflectUtil.getVal(sourceVo,"source1");
			Object cost = ReflectUtil.getVal(sourceVo,"cash");
			JSONObject item = new JSONObject();
			item.put("value", cost);
			item.put("name", sourceName);
			dataList.add(item);
			source1List.add(sourceName);
		}
//		String[] sources = {"直接访问","联盟广告","视频广告","搜索引擎","邮件营销"};
//		Random random = new Random();
//		
//      for(String f : sources){
//			JSONObject item = new JSONObject();
//			item.put("value", random.nextInt(10000)+9000);
//			item.put("name", f);
//			dataList.add(item);
//			source1List.add(f);
//      }
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("source1NameList", source1List);
		jsonObject.put("dataList", dataList);
		
		return jsonObject.toString();
	}	
	

	/**
	 * 获取流量2级来源  “饼图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	public static String getSource2PieDataJson(String businessName,String source1,IncomePlatConst plat,String beginDate,String endDate){
		
		Class<?> beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSource2PieCon(businessName, source1, beginDate, endDate);
		String columns = IncomePlatConst.getSource2PieCols();
		List<?> list = service.getListByPage(condition, columns, 1, 5, "cash desc");
		
		
		List<Object> source1List = Lists.newArrayList();
		List<Object> dataList = Lists.newArrayList();
		
		for (Object  sourceVo: list) {
			Object sourceName = ReflectUtil.getVal(sourceVo,"source2");
			Object cost = ReflectUtil.getVal(sourceVo,"cash");
			JSONObject item = new JSONObject();
			item.put("value", cost);
			item.put("name", sourceName);
			dataList.add(item);
			source1List.add(sourceName);
		}
//		String[] sources = {"直接访问","联盟广告","视频广告","搜索引擎","邮件营销"};
//		Random random = new Random();
//		
//      for(String f : sources){
//			JSONObject item = new JSONObject();
//			item.put("value", random.nextInt(10000)+9000);
//			item.put("name", f);
//			dataList.add(item);
//			source1List.add(f);
//      }
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("source2NameList", source1List);
		jsonObject.put("dataList", dataList);
		return jsonObject.toString();
	}	

	
	/**
	 * 获取流量0级来源  “曲线图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getSourceLineDataJson(String businessName, String dateGroup,String stat_type, String sources,IncomePlatConst plat,String index){
		Iterable<String> topNSourcesArray = Splitter.on("','").split(sources);
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSourceLineCon(businessName, stat_type);
		String columns = IncomePlatConst.getSourceLineCols(dateGroup,index);
		List list = service.getListByConditon(columns, condition, null);

		HashMap<String, List<Object>> dataMap = new HashMap<String, List<Object>>(); // cate : [dataList]
		TreeMap<String, List<?>> stat_dateMap = new TreeMap<String, List<?>>(ReflectUtil.groupByField(list,dateGroup));
		// vppv/uv比较特殊
		for (String date : stat_dateMap.keySet()) {
			for (String src : topNSourcesArray) {
				List<Object> curList = dataMap.get(src);
				if(curList == null)
					curList = Lists.newArrayList();
				Object nf = ReflectUtil.getByColVal(beanClas,stat_dateMap.get(date),"source",src);
				Object value = ReflectUtil.getVal(nf, index);
				curList.add(value == null ? 0 : value);
				dataMap.put(src, curList);
			}
		}
		
//	    List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//		Random random = new Random();
//		
//	    for(String src : topNSourcesArray){
//	    	List<Object> dataList =Lists.newArrayList();
//	    	for(int i=0;i<dayList.size();i++){
//	            dataList.add(random.nextInt(10000)+9000);
//	        }
//	    	dataMap.put(src, dataList);
//	    }
		
		JSONObject jsonObject = new JSONObject(); 	
//		jsonObject.put("time", dayList);
		jsonObject.put("time", stat_dateMap.keySet());
		jsonObject.putAll(dataMap);
		return jsonObject.toString();
	}
	
		
	/**
	 * 获取流量一级来源  “曲线图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getSource1LineDataJson(String businessName, String dateGroup,String stat_type, String source, String source1s,IncomePlatConst plat,String index){
		Iterable<String> topNSourcesArray = Splitter.on("','").split(source1s);
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSource1LineCon(businessName, stat_type, source, source1s);
		String columns = IncomePlatConst.getSource1LineCols(dateGroup,index);
		List list = service.getListByConditon(columns, condition, null);

		HashMap<String, List<Object>> dataMap = new HashMap<String, List<Object>>(); // cate : [dataList]
		TreeMap<String, List<?>> stat_dateMap = new TreeMap<String, List<?>>(ReflectUtil.groupByField(list,dateGroup));
		// vppv/uv比较特殊
		for (String date : stat_dateMap.keySet()) {
			for (String src : topNSourcesArray) {
				List<Object> curList = dataMap.get(src);
				if(curList == null)
					curList = Lists.newArrayList();
				Object nf = ReflectUtil.getByColVal(beanClas,stat_dateMap.get(date),"source1",src);
				Object value = ReflectUtil.getVal(nf, index);
				curList.add(value == null ? 0 : value);
				dataMap.put(src, curList);
			}
		}
		
//	    List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//		Random random = new Random();
//		
//	    for(String src : topNSourcesArray){
//	    	List<Object> dataList =Lists.newArrayList();
//	    	for(int i=0;i<dayList.size();i++){
//	            dataList.add(random.nextInt(10000)+9000);
//	        }
//	    	dataMap.put(src, dataList);
//	    }
		
		JSONObject jsonObject = new JSONObject(); 	
//		jsonObject.put("time", dayList);
		jsonObject.put("time", stat_dateMap.keySet());
		jsonObject.putAll(dataMap);
		return jsonObject.toString();
	}


	/**
	 * 获取流量2级来源  “曲线图” 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getSource2LineDataJson(String businessName, String dateGroup,String stat_type, String source1, String source2s,IncomePlatConst plat,String index){
		Iterable<String> topNSourcesArray = Splitter.on("','").split(source2s);
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition = IncomePlatConst.getSource2LineCon(businessName, stat_type, source1, source2s);
		String columns = IncomePlatConst.getSource2LineCols(dateGroup,index);
		List list = service.getListByConditon(columns, condition, null);

		HashMap<String, List<Object>> dataMap = new HashMap<String, List<Object>>(); // cate : [dataList]
		TreeMap<String, List<?>> stat_dateMap = new TreeMap<String, List<?>>(ReflectUtil.groupByField(list,dateGroup));
		// vppv/uv比较特殊
		for (String date : stat_dateMap.keySet()) {
			for (String src : topNSourcesArray) {
				List<Object> curList = dataMap.get(src);
				if(curList == null)
					curList = Lists.newArrayList();
				Object nf = ReflectUtil.getByColVal(beanClas,stat_dateMap.get(date),"source2",src);
				Object value = ReflectUtil.getVal(nf, index);
				curList.add(value == null ? 0 : value);
				dataMap.put(src, curList);
			}
		}
		
//	    List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
//		Random random = new Random();
//		
//	    for(String src : topNSourcesArray){
//	    	List<Object> dataList =Lists.newArrayList();
//	    	for(int i=0;i<dayList.size();i++){
//	            dataList.add(random.nextInt(10000)+9000);
//	        }
//	    	dataMap.put(src, dataList);
//	    }
		
		JSONObject jsonObject = new JSONObject(); 	
//		jsonObject.put("time", dayList);
		jsonObject.put("time", stat_dateMap.keySet());
		jsonObject.putAll(dataMap);
		return jsonObject.toString();
	}
  
    /**
     * echarts点击0级来源表格数据
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getSourceLineTableJson(String businessName,
			String dateGroup, String stat_type, String dateVal, String sources, IncomePlatConst plat) {
	      
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition =  IncomePlatConst.getSourceTabCon(businessName, stat_type,dateGroup,dateVal);
	    List<?> list = service.getListByConditon( null, condition, "cash desc");

	     JSONObject ob = new JSONObject();
	     ob.put("dataList", list);

	      return ob.toString();
	}	
	
    /**
     * echarts点击1级来源表格数据
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getSource1LineTableJson(String businessName,
			String dateGroup, String stat_type, String dateVal, String source,
			String source1s, IncomePlatConst plat) {
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition =  IncomePlatConst.getSource1TabCon(businessName, stat_type,dateGroup,dateVal, source, source1s);
	    List<?> list = service.getListByConditon( null, condition, "cash desc");

	    JSONObject ob = new JSONObject();
	    ob.put("dataList", list);
	    return ob.toString();
	}	
	
    /**
     * echarts点击2级来源表格数据
     */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getSource2LineTableJson(String businessName,
			String dateGroup, String stat_type, String dateVal, String source1,
			String source2s, IncomePlatConst plat) {
		Class beanClas= plat.getSourceBeanCls();
		AbstractBaseService<?>  service = ServiceBeanFactory.get(beanClas,null); //根据实体类获取对应的service
		String condition =  IncomePlatConst.getSource2TabCon(businessName, stat_type,dateGroup,dateVal, source1, source2s);
	    List<?> list = service.getListByConditon( null, condition, "cash desc");

	    JSONObject ob = new JSONObject();
	     ob.put("dataList", list);

	     return ob.toString();
	}	
}
